package first.progr.tc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import com.thoughtworks.xstream.XStream;

import static first.progr.tc.TestBase.generateRandStr;;

public class ContactDataGenerator{

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter string in format:<acount od test data> <file> <format>");
		String[] inputString = in.nextLine().split(" ");
		   if (inputString.length < 3) {
		      System.out.println("Format of entered line is incorrect. Please repeat entering one more time ");
		      return;
	       } 
	     int amount = Integer.parseInt(inputString[0]);
	     File file = new File(inputString[1]);
	     String format = inputString[2];
		
	     if (file.exists()) {
		    System.out.println("File " + file + " already exists, please remove it manualy");
		    return;
	     }
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			   saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			   saveContactsToXmlFile(contacts, file);  
		} else {
			   System.out.println("Unknown " + format + " file format ");
			   return;
		}
		System.out.println("File " + file + " was sucessfully generated");
	}
	
	 public static List<ContactData> generateRandomContacts(int amount) {
		 List<ContactData> list = new ArrayList<ContactData>();
		    for (int i = 0; i < amount-1; i++) {
		    	ContactData cont = new ContactData()
		    	  .withFitstName(generateNotEmptyRandStr())
		    	  .withLastName(generateNotEmptyRandStr())
		          .withAddress(generateRandStr())
		          .withHomePhone(generateRandPhoneNum())
		          .withMobilePhone(generateRandPhoneNum())
		          .withWorkPhone(generateRandPhoneNum())
		          .withMainEmail(generateRandEmail())
		          .withExtraEmail(generateRandEmail())
		          .withBirthDay(generateRandBDay())
		          .withBirthMonth(generateRandMonth())
	              .withBirthYear(generateRandNum())	 
		          .withSecondAddress(generateRandStr())
		          .withHomeWebPage(generateRandWebPage());
		    	list.add(cont);
		    }
		    for (int j = amount-1; j < amount; j++) {
		    	ContactData cont = new ContactData()
		    	  .withFitstName("")
		    	  .withLastName("")
		          .withAddress("")
		          .withHomePhone("")
		          .withMobilePhone("")
		          .withWorkPhone("")
		          .withMainEmail("")
		          .withExtraEmail("")
		          .withBirthDay("-")
		          .withBirthMonth("-")
	              .withBirthYear("")
		          .withSecondAddress("")
		          .withHomeWebPage("");
		    	list.add(cont);
		    }
		    return list;
	 }
	
	 private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException {
		FileWriter writerF = new FileWriter(file);
		for (ContactData cont1 : contacts){
			writerF.write(cont1.getFirstName() + "," + cont1.getLastName() + "," + cont1.getAddress() + "," + cont1.getHomePhone() + "," + cont1.getMobilePhone() + "," + cont1.getWorkPhone() + "," + 
		                  cont1.getMainEmail() + "," + cont1.getExtraEmail() + "," + cont1.getBirthDay() + "," + cont1.getBirthMonth() + "," + cont1.getBirthYear() + "," + cont1.getSecondAddress() + "," + cont1.getHomeWebPage() + ",!" + "\n");
		}
		writerF.close();
   	 }

	 public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		  List<ContactData> list = new ArrayList<ContactData>();
		  FileReader readerF = new FileReader(file);
		  BufferedReader bufferedReader = new BufferedReader(readerF);
		  String line = bufferedReader.readLine();
		  while (line != null) {
			  String[] part = line.split(",");
			  ContactData cont1 = new ContactData()
			    .withFitstName(part[0])
			    .withLastName(part[1])
			    .withAddress(part[2])
			    .withHomePhone(part[3])
			    .withMobilePhone(part[4])
			    .withWorkPhone(part[5])
			    .withMainEmail(part[6])
			    .withExtraEmail(part[7])
			    .withBirthDay(part[8])
			    .withBirthMonth(part[9])
			    .withBirthYear(part[10])
			    .withSecondAddress(part[11])
			    .withHomeWebPage(part[12]);
			  list.add(cont1);
			  line = bufferedReader.readLine();
		  }
		  bufferedReader.close();
		  return list;
	  }
	 
	 private static void saveContactsToXmlFile (List<ContactData> contacts, File file) throws IOException {
		  XStream xsParser = new XStream();
		  xsParser.alias("contact", ContactData.class);
		  String xml = xsParser.toXML(contacts);
		  FileWriter writerF = new FileWriter(file);
		  writerF.write(xml);
		  writerF.close();
	  }
	 
	 public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException {
		  XStream xstr = new XStream();
		  xstr.alias("contact", ContactData.class);
		  return (List<ContactData>) xstr.fromXML(file);
	 }
	  
	 //-----------------------------------------------------------------------------------
	 
	 public static String generateNotEmptyRandStr(){
			Random rnd = new Random();
			return "Data" + rnd.nextInt();
	 }
		
	 public static String generateRandPhoneNum(){
			Random rnd = new Random();
			return "+" + rnd.nextInt(9) + "(" + rnd.nextInt(333) + ")" + rnd.nextInt(44) + "-" + rnd.nextInt(44) + "-" + rnd.nextInt(44);
     }
		
	 public static String generateRandNum(){
			Random rnd = new Random();
			return "" + rnd.nextInt(3000);
	 }
		
	 public static String generateRandEmail(){
			Random rnd = new Random();
			return "web" + rnd.nextInt(100) + "@example.com";
	 }
		
	 public static String generateRandBDay(){
			Random rnd = new Random();
			return "" + rnd.nextInt(31);
	 }
		
	 public static String generateRandWebPage(){
			Random rnd = new Random();
			return "WebPage" + rnd.nextInt(100) + "@google.com";
	 }
		
	 public static String generateRandMonth(){
			Random rnd = new Random();
			String[] month = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
			int index = rnd.nextInt(month.length);
			return month[index];
	 }

}
